<?php
class connection{
         function __construct($connection=NULL){
                if($connection=="cashier"){
                     $this->connection = mysqli_connect("localhost","root","webteam2012","ssc_cash_sis");
                }elseif($connection=="registrar"){
                    $this->connection = mysqli_connect("localhost","root","webteam2012","ssc_reg_sis");
                }elseif($connection=="information_schema"){
                    $this->connection = mysqli_connect("localhost","root","webteam2012","information_schema");
                }else{
                    return false;
                }
         } 
         
        function open(){
            return $this->connection;
        }

        function close(){
            return mysqli_close($this->connection);
        }

        function read($id){
            $query = sql_functions::select_query("* from session_table where id='".$id."'");
            $count= sql_functions::count_rows($query);
            if($count==0){
                return '';
            }else{
                $data=sql_functions::fetch_to_array($query);
                return $data[0]['data'];
            }
        }

        function write($id,$data){
            sql_functions::apply_query("replace into session_table(id,data)values('".$id."','".$data."')");
            return mysqli_affected_rows($this->connection);
        }

        function destroy($id){
            sql_functions::apply_query("delete from session_table where id='".$id."'");
            return mysqli_affected_rows($this->connection);
        }

        function gc(){
            sql_functions::apply_query("delete from session_table where date_add(batog,interval 1 minute)<now()");
            return mysqli_affected_rows($this->connection);
        }



        public function start_session(){
            $connection = new connection("registrar");
            session_set_save_handler(
                array($connection, 'open'),
                array($connection, 'close'),
                array($connection, 'read'),
                array($connection, 'write'),
                array($connection, 'destroy'),
                array($connection, 'gc')
            );
            session_start();
        }
}
?>
